//
//  LVImageViewController.m
//  LEVE
//
//  Created by 王若聪 on 2016/12/6.
//  Copyright © 2016年 dashuju. All rights reserved.
//

#import "LVImageViewController.h"
#import "ViewUtils.h"
@import AssetsLibrary;

@interface LVImageViewController ()<UIPickerViewDataSource,UIPickerViewDelegate>
@property (strong, nonatomic) UIImage *image;
@property (strong, nonatomic) UIImageView *imageView;

@property (nonatomic,strong)UIPickerView *timeSetterPickerView;
@property (nonatomic,strong)NSArray *timeSetterArray;

//@property (strong, nonatomic) UILabel *infoLabel;
@end

@implementation LVImageViewController
{
    UIImage *_drawnImage;
}
- (instancetype)initWithImage:(UIImage *)image {
    self = [super init];
    if(self) {
        _image = image;


    }
    
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.imageView.backgroundColor = [UIColor blackColor];
    
    CGRect screenRect = [[UIScreen mainScreen] bounds];
    
    self.imageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, screenRect.size.width, screenRect.size.height)];
    self.imageView.contentMode = UIViewContentModeScaleAspectFit;
    self.imageView.backgroundColor = [UIColor clearColor];
    self.imageView.image = self.image;
    [self.view addSubview:self.imageView];

    
    
    //    NSString *info = [NSString stringWithFormat:@"Size: %@  -  Orientation: %ld", NSStringFromCGSize(self.image.size), (long)self.image.imageOrientation];
    //    self.infoLabel = [[UILabel alloc] initWithFrame:CGRectMake(0, 0, 100, 20)];
    //    self.infoLabel.backgroundColor = [[UIColor darkGrayColor] colorWithAlphaComponent:0.7];
    //    self.infoLabel.textColor = [UIColor whiteColor];
    //    self.infoLabel.font = [UIFont fontWithName:@"AvenirNext-Regular" size:13];
    //    self.infoLabel.textAlignment = NSTextAlignmentCenter;
    //    self.infoLabel.text = info;
    //    [self.view addSubview:self.infoLabel];
    
    
    //    [self setupJotViewController];
}

- (void)saveButtonPressed:(UIButton *)button {
    //保存照片
    UIImage *drawnImage = [self.jotViewController drawOnImage:_image];
    _drawnImage = drawnImage ? drawnImage : _image;
    //    UIImage *drawnImage = [self.jotViewController renderImageWithScale:2.f
    //                                                               onColor:self.view.backgroundColor];
    
    
    ALAssetsLibrary *library = [ALAssetsLibrary new];
    [library writeImageToSavedPhotosAlbum:[drawnImage CGImage]
                              orientation:(ALAssetOrientation)[drawnImage imageOrientation]
                          completionBlock:^(NSURL *assetURL, NSError *error){
                              if (error) {
                                  NSLog(@"Error saving photo: %@", error.localizedDescription);
                              } else {
                                  NSLog(@"Saved photo to saved photos album.");
                              }
                          }];
    
    [self.jotViewController clearAll];

    [self dismissViewControllerAnimated:NO completion:nil];
    
}
- (void)sendButtonPressed:(UIButton *)btn
{
    if (self.sendBlock) {
        if (!_drawnImage) {
            _drawnImage = [self.jotViewController drawOnImage:_image];
            if (!_drawnImage) {
                _drawnImage = _image;
            }
        }
        self.sendBlock(_drawnImage);
        self.sendBlock = nil;
    }
}
- (void)tapBackgroundWhenSetTime:(UITapGestureRecognizer *)recognizer {
    
    if (_timeSetterPickerView) {
        _timeSetterPickerView.hidden = YES;
        [_timeSetterPickerView removeFromSuperview];
        _timeSetterPickerView = nil;
    }
    
}

- (void)timeSetterButtonPressed:(UIButton *)button {
    NSLog(@"timeSetter Button clicked");
    
    // 初始化pickerView
    self.timeSetterPickerView = [[UIPickerView alloc]init];
    self.timeSetterPickerView.frame = CGRectMake(0, kScreenHeight - 200, kScreenWidth, 200);
    self.timeSetterPickerView.backgroundColor = [UIColor colorWithRed:245/255.0 green:244/255.0 blue:244/255.0 alpha:1];
    [self.view addSubview:self.timeSetterPickerView];
    UILabel *label2 = [[UILabel alloc]init];
    label2.frame = CGRectMake(self.timeSetterPickerView.bounds.size.width/2, self.timeSetterPickerView.bounds.size.height/2 - 20, 40, 40);
    label2.font = [UIFont systemFontOfSize:24];
    label2.textAlignment = NSTextAlignmentLeft;
    label2.text = @"S";
    [self.timeSetterPickerView addSubview:label2];
    
    self.timeSetterPickerView.delegate = self;
    self.timeSetterPickerView.dataSource = self;
    
    
    UITapGestureRecognizer *tapbackgroundRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(tapBackgroundWhenSetTime:)];
    [tapbackgroundRecognizer setNumberOfTapsRequired:1];
    [self.view addGestureRecognizer:tapbackgroundRecognizer];
}
#pragma mark UIPickerView DataSource Method

-(NSInteger)numberOfComponentsInPickerView:(UIPickerView *)pickerView{
    return 1;
}

-(NSInteger)pickerView:(UIPickerView *)pickerView numberOfRowsInComponent:(NSInteger)component{
    return self.timeSetterArray.count;
}

#pragma mark UIPickerView Delegate Method
- (UIView *)pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent:(NSInteger)component reusingView:(UIView *)view {
    UIView *pickerCellView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 40)];
    pickerCellView.backgroundColor = [UIColor whiteColor];
    UILabel *label = [[UILabel alloc]initWithFrame:CGRectMake(0, 0, kScreenWidth/2, 40)];
    label.font = [UIFont boldSystemFontOfSize:26];
    label.textAlignment = NSTextAlignmentRight;
    label.text = self.timeSetterArray[row];
    [pickerCellView addSubview:label];
    return pickerCellView;
}

- (CGFloat)pickerView:(UIPickerView *)pickerView rowHeightForComponent:(NSInteger)component{
    
    return 40;
    
}

//-(NSString *)pickerView:(UIPickerView *)pickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component{
//    return self.timeSetterArray[row];
//}

-(void)pickerView:(UIPickerView *)pickerView didSelectRow:(NSInteger)row inComponent:(NSInteger)component{
    NSString *timeString = (NSString *)self.timeSetterArray[row];
    unsigned int time = [timeString intValue];
    [self setTimeSetterButtonText:time];
}

- (void)viewWillLayoutSubviews {
    [super viewWillLayoutSubviews];
    
    self.imageView.frame = self.view.bounds;
    
    //    [self.infoLabel sizeToFit];
    //    self.infoLabel.width = self.view.contentBounds.size.width;
    //    self.infoLabel.top = self.view.contentBounds.size.height - self.infoLabel.height;
    //    self.infoLabel.left = 0;
    
    [self.timeSetterButton addTarget:self action:@selector(timeSetterButtonPressed:) forControlEvents:UIControlEventTouchUpInside];
    self.timeSetterButton.frame = CGRectMake(self.saveButton.right + 2 ,self.saveButton.y, 50, 70);
    [self.view addSubview:self.timeSetterButton];
    
    if (self.timeSetterPickerView) {
        [self.view bringSubviewToFront:self.timeSetterPickerView];
    }
    
    if (self.swipeTableView) {
        //        [self.view bringSubviewToFront:self.segmentBar];
        
        //        self.cancelButton.hidden = YES;
        self.saveButton.hidden = YES;
        self.timeSetterButton.hidden = YES;
    }else{
        self.saveButton.hidden = NO;
        self.timeSetterButton.hidden = NO;
    }
}

- (UIButton *)timeSetterButton {
    if(!_timeSetterButton) {
        UIImage *saveImage = [UIImage imageNamed:@"camera_time"];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tintColor = [UIColor whiteColor];
        [button setImage:saveImage forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont boldSystemFontOfSize:13];
        [button setTitle:@"3" forState:UIControlStateNormal];
        [button setTitleEdgeInsets:UIEdgeInsetsMake(10, -25, 7, 7)];
        button.imageView.clipsToBounds = NO;
        button.imageEdgeInsets = UIEdgeInsetsMake(14, 9, 14, 9);
        button.layer.shadowColor = [UIColor blackColor].CGColor;
        button.layer.shadowOffset = CGSizeMake(0.0f, 0.0f);
        button.layer.shadowOpacity = 0.4f;
        button.layer.shadowRadius = 1.0f;
        button.clipsToBounds = NO;
        _timeSetterButton = button;
    }
    return _timeSetterButton;
}

- (NSArray *)timeSetterArray {
    if(!_timeSetterArray) {
        _timeSetterArray = @[@"1",@"2",@"3",@"4",@"5",@"6",@"7",@"8",@"9",@"10"];
    }
    return _timeSetterArray;
}

- (void)setTimeSetterButtonText:(unsigned int)time{
    if (_timeSetterButton) {
        [_timeSetterButton setTitle:[NSString stringWithFormat:@"%d",time] forState:UIControlStateNormal];
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    
}

@end
